package com.chookss.video

import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.*
import android.view.WindowManager.LayoutParams
import android.widget.EditText
import androidx.appcompat.app.AppCompatDialog
import com.chookss.R
import com.chookss.tools.Utils
import com.chookss.view.listener.CommentInputDialogListener

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/22 14:22
 * Note:
 */
class VideoCommentsInputDialog(context: Context,type:String) : AppCompatDialog(context, R.style.DialogFullWidthDimEnabled) {

    var inputAction:((String)->Unit)? = null
    var editText:EditText? = null
    var type:String? = type  //1正常2要回过去接口成功
    var listener: CommentInputDialogListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.view_video_comment_input)
        setCanceledOnTouchOutside(true)
        setCancelable(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setGravity(Gravity.BOTTOM) //dialog底部弹出
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window?.setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE or LayoutParams.SOFT_INPUT_STATE_VISIBLE)
        window?.setWindowAnimations(R.style.DialogBottomAnim)
        editText = findViewById(R.id.edit)!!
        if("3".equals(type)){
            editText?.hint="回复评论"
        }
        findViewById<View>(R.id.ivSend)?.setOnClickListener {
            var text:String = editText?.text.toString().trim()
            if (!Utils.isNull(text)) {
                inputAction?.invoke(text)
                if (listener != null) {
                    listener!!.onClick( text)
                }
                dismiss()
            }
        }
        setOnKeyListener(keylistener)
    }

    override fun onStart() {
        super.onStart()
        editText?.requestFocus()
    }

    var keylistener: DialogInterface.OnKeyListener = DialogInterface.OnKeyListener { dialog, keyCode, event ->
        Log.i("TAG", "键盘code---$keyCode")
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialog.dismiss()
            false
        } else if (keyCode == KeyEvent.KEYCODE_DEL) { //删除键
            false
        } else {
            true
        }
    }

}